<template>
	<view>
    <!-- 搜索组件 -->
    <view class="search-box">
      <my-search  @click="btnSearch"></my-search>
      
    </view>
    <!-- 轮播图区域 -->
		<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular="true">
      <swiper-item v-for="(item , i) in swiperList" :key="i">
        <navigator class="swiper-item" :url="'/subpkg/goods-detail/goods-detail?goods_id=' + item.goods_id">
            <image :src="item.image_src"></image>
        </navigator>
      </swiper-item>
    </swiper>
    <!-- 导航分类列表 -->
    <!-- 给item项添加点击事件 -->
    <view class="nav-list">
     <view class="nav-item" v-for="(item , i) in navList" :key="i" @click="btnHandle(item)">
       <image :src="item.image_src" ></image>
       <!-- <text>{{item.name}}</text> -->
     </view>
    </view>
    <!-- 楼层区域 -->
    <view class="floor-list">
      <view class="floor-item" v-for="(item , i) in floorList" :key="i">
        <!-- 标题 -->
        <image :src="item.floor_title.image_src" class="floor-title"></image>
        <!-- 图片 -->
        <view class="fllor-image-box">
          <!-- 左侧图片 -->
          <navigator class="left" :url="item.product_list[0].url">
            <image :src="item.product_list[0].image_src" :style="{width:item.product_list[0].image_width + 'rpx' }" mode="widthFix"></image>
            <!-- <image :src="item.product_list[0].image_src" :style="{width:item.product_list[0].image_width + 'rpx' mode=""></image> -->
          </navigator>
          <!-- 右侧图片 -->
          <view class="right">
            <navigator v-for="(item2 , index) in item.product_list" :key="index" v-if="index !== 0" :url="item2.url">
              <image :src="item2.image_src" :style="{width:item2.image_width + 'rpx' }" mode="widthFix"></image>
            </navigator>
          </view>
        </view>
      </view>
    </view>
	</view>
</template>

<script>
  import badgeMix from '../../mixin-tabbar-badge/tabbar_badge.js'
	export default {
     mixins:[badgeMix],
		data() {
			return {
				//轮播图
        swiperList:[],
        //首页分类导航
        navList:[],
        //楼层数据
        floorList:[]
			};
		},
    onLoad() {
      this.getSwiperList(),
      this.getNavList(),
      this.getFloorList()
    },
    methods:{
      //获取轮播图
   async getSwiperList(){
     const res = await uni.$http.get('/home/swiperdata')
     // console.log(res)
     //请求失败处理
     if(res.data.meta.status !==200){
      return uni.showMsg('获取数据失败',1500)
     }
     //成功后将数据赋值给swiperList
     this.swiperList = res.data.message
     uni.showMsg('数据获取成功',1500)
    },
    //获取首页分类导航
    async getNavList(){
     const res = await uni.$http.get("/home/catitems")
     // console.log(res.data.message)
     if(res.data.meta.status !==200){
      return uni.showMsg('获取数据失败',1500)
     }
     this.navList = res.data.message
     // console.log(this.navList)
     },
     //点击跳转到分类tabBar页面
     btnHandle(item){
       // console.log(item);
       //判断name=分类跳转
       if(item.name==="分类"){
         uni.switchTab({
           url:'/pages/cate/cate'
         })
       }
     },
     //获取楼层数据
     async getFloorList(){
      const res = await uni.$http.get("/home/floordata")
      if(res.data.meta.status !==200){
        return uni.showMsg('数据获取失败' , 1500)
      }
      //对获取到的数据进行处理
      res.data.message.forEach(floor => {
        floor.product_list.forEach(prod => {
          // console.log(prod);
          prod.url = '/subpkg/goods-list/goods-list?'+prod.navigator_url.split('?')[1]
        })
      })
      this.floorList = res.data.message
           console.log(this.floorList);
     },
     btnSearch(){
       uni.navigateTo({
         url:'/subpkg/search/search'
       })
     }
    },
    // onShow() {
    //   console.log('ok');
    // }
	}
</script>

<style lang="scss">
swiper {
  height: 330rpx;
  .swiper-item,image{
    width: 100%;
    height: 100%;
  }
}
.nav-list {
  display: flex;
  justify-content: space-between;
  margin: 15rpx 0;
  .nav-item,image {
    width: 128rpx;
    height: 140rpx;
  }
}
.floor-title {
  display: flex;
  height: 60rpx;
  width: 100%;
}
.fllor-image-box {
  display: flex;
  padding-left:15rpx ;
}
.right{
    display: flex;
    flex-wrap: wrap;
    justify-content: space-around;
  }
  .search-box {
    position: sticky;
    top: 0;
    z-index: 999;
  }
</style>
